package com.mt.roll.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mt.roll.core.api.Assert;
import com.mt.roll.core.base.BaseService;
import com.mt.roll.core.dal.entity.ScrapInformation;
import com.mt.roll.core.dal.service.ScrapInformationServiceBiz;
import com.mt.roll.core.enums.WhetherEnum;
import com.mt.roll.core.params.EnabledParam;
import com.mt.roll.core.params.IdListParam;
import com.mt.roll.core.params.IdParam;
import com.mt.roll.core.utils.CodeGeneratorHelper;
import com.mt.roll.core.vo.IdListVo;
import com.mt.roll.core.vo.IdVo;
import com.mt.roll.core.vo.PageVo;
import com.mt.roll.core.vo.R;
import com.mt.roll.wms.params.ScrapInformationParam;

import com.mt.roll.wms.params.ScrapInformationQueryParam;
import com.mt.roll.wms.service.ScrapInformationService;
import com.mt.roll.wms.vo.ScrapInformationVo;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author weihongyang
 * @Date 2021/3/10 9:12 上午
 * @Version 1.0
 */
@Service
@Transactional
public class ScrapInformationServiceImpl extends BaseService implements ScrapInformationService {
    @Autowired
    private ScrapInformationServiceBiz scrapInformationServiceBiz;


    /**
     * 获取xx
     *
     * @param idParam 主键参数
     * @return xx
     */
    @Override
    public R<ScrapInformationVo> get(IdParam idParam) {
        ScrapInformation scrapInformation = scrapInformationServiceBiz.getById(idParam.getId());
        Assert.notNull("报废信息不存在", scrapInformation);
        ScrapInformationVo scrapInformationVo = ScrapInformationVo.builder().build();
        BeanUtils.copyProperties(scrapInformation, scrapInformationVo);
        return successful(scrapInformationVo);
    }

    /**
     * 获取xx列表
     *
     * @param scrapInformationQueryParam xx查询参数
     * @return xx列表
     */
    @Override
    public R<List<ScrapInformationVo>> list(ScrapInformationQueryParam scrapInformationQueryParam) {
        return null;
    }

    /**
     * 获取xx分页列表
     *
     * @param scrapInformationQueryParam xx查询参数
     * @return xx分页列表
     */
    @Override
    public R<PageVo<ScrapInformationVo>> page(ScrapInformationQueryParam scrapInformationQueryParam) {
        QueryWrapper<ScrapInformation> wrapper = new QueryWrapper<>();
        wrapper.eq(ObjectUtils.isNotNull(scrapInformationQueryParam.getEnabled()), ScrapInformation.ENABLED, scrapInformationQueryParam.getEnabled())
                .eq(ObjectUtils.isNotNull(scrapInformationQueryParam.getScrapId()), ScrapInformation.SCRAP_ID, scrapInformationQueryParam.getScrapId())
                .like(StringUtils.isNotBlank(scrapInformationQueryParam.getScrap()), ScrapInformation.SCRAP, scrapInformationQueryParam.getScrap());
        wrapper.orderByDesc(ScrapInformation.CREATE_TIME);
        IPage<ScrapInformation> page = scrapInformationServiceBiz.page(new Page<>(scrapInformationQueryParam.getCurrent(), scrapInformationQueryParam.getSize()), wrapper);
        return successful(new PageVo<>(page, ScrapInformationVo.class));
    }

    /**
     * 获取xx树列表
     *
     * @param scrapInformationQueryParam xx查询参数
     * @return xx树列表
     */
    @Override
    public R<List<ScrapInformationVo>> tree(ScrapInformationQueryParam scrapInformationQueryParam) {
        return null;
    }

    /**
     * 新增xx
     *
     * @param scrapInformationParam xx参数
     * @return 主键
     */
    @Override
    public R<IdVo> add(ScrapInformationParam scrapInformationParam) {
        QueryWrapper<ScrapInformation> wrapper = new QueryWrapper<>();
        wrapper.eq(ScrapInformation.CODE, scrapInformationParam.getCode());
        Assert.eqZero(scrapInformationServiceBiz.count(wrapper), "报废信息编码已经存在");
        ScrapInformation scrapInformation = new ScrapInformation();
        BeanUtils.copyProperties(scrapInformationParam, scrapInformation);
        setCommonField(scrapInformation);
        scrapInformationServiceBiz.save(scrapInformation);
        return successful(IdVo.builder().id(scrapInformation.getId()).build());
    }

    /**
     * 更新xx
     *
     * @param scrapInformationParam xx参数
     * @return 主键
     */
    @Override
    public R<IdVo> update(ScrapInformationParam scrapInformationParam) {
        ScrapInformation scrapInformation = scrapInformationServiceBiz.getById(scrapInformationParam.getId());
        //修改报废编码需要检验新编码是否已存在
        if (!scrapInformation.getCode().equals(scrapInformationParam.getCode())) {
            QueryWrapper<ScrapInformation> wrapper = new QueryWrapper<>();
            wrapper.eq(ScrapInformation.CODE, scrapInformationParam.getCode());
            Assert.eqZero(scrapInformationServiceBiz.count(wrapper), "报废信息编码已经存在");
        }
        ScrapInformation updateScrap = new ScrapInformation();
        BeanUtils.copyProperties(scrapInformationParam, updateScrap);
        setUpdateCommonField(updateScrap);
        scrapInformationServiceBiz.updateById(updateScrap);
        return successful(IdVo.builder().id(scrapInformation.getId()).build());
    }

    /**
     * 启用停用
     *
     * @param enabledParam 启停参数
     * @return 主键
     */
    @Override
    public R<IdVo> enabled(EnabledParam enabledParam) {
        return null;
    }

    /**
     * 删除工厂
     *
     * @param idParam 主键参数
     * @return 主键
     */
    @Override
    public R<IdVo> delete(IdParam idParam) {
        ScrapInformation scrapInformation = scrapInformationServiceBiz.getById(idParam.getId());
        if (scrapInformation == null) {
            return failed("报废信息不存在");
        }
        //使用更新方式删除，需要把code设置为历史code（后面跟随机数）
        ScrapInformation updateScrap = new ScrapInformation().setId(scrapInformation.getId());
        updateScrap.setCode(scrapInformation.getCode() + "_" + RandomUtils.nextInt());
        updateScrap.setValid(WhetherEnum.NO.getValue());
        setUpdateCommonField(updateScrap);
        scrapInformationServiceBiz.updateById(updateScrap);
        //更新时逻辑删除字段没有更新，需要再调用一次删除操作
        scrapInformationServiceBiz.removeById(updateScrap.getId());
        return successful(IdVo.builder().id(scrapInformation.getId()).build());
    }

    /**
     * 批量删除xx
     *
     * @param idListParam 主键列表参数
     * @return 主键列表
     */
    @Override
    public R<IdListVo> delete(IdListParam idListParam) {
        return null;
    }

    @Override
    public R<String> getCode() {
        return successful(CodeGeneratorHelper.getScrapInformationCode());
    }
}
